package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.dao.MenuMapper;
import com.qf.dao.RoleMapper;
import com.qf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/21
 * 键盘敲烂，月薪过万
 * 登高必自卑，行远必自迩。
 * 不忘初心：像初学者一样拥有一颗谦虚、开放的心态。
 * 李学峰
 */
@Service
public class RoleServiceImpl implements RoleService {
   @Autowired
   private RoleMapper roleMapper;
   @Autowired
   private MenuMapper menuMapper;


    @Override
    public PageInfo<Menu> findmenus(int pageIndex, int pagesize, String title, String deptname, int status) {

        PageHelper.startPage(pageIndex,pagesize);
        Map map=new HashMap();
        map.put("title",title);
        map.put("dname",deptname);
        map.put("status",status);
        //3.查询数据
        List<Menu> menuList = roleMapper.findall(map);
        //4.返回pageInfo
        return new PageInfo(menuList);
    }

    @Override
    public List<Role> findbyDeptid(int did) {
        return roleMapper.findbyDeptid(did);
    }


    //lxf删除
    @Override
    public int deleteByPrimaryKey(Integer roleid) {
        int i = roleMapper.deleteByPrimaryKey(roleid);
        return i;
    }

    @Override
    @Transactional
    public int insert(Role record,int[] mid) {

        int insert = roleMapper.insert(record);
        Integer roleid = record.getRoleid();
        HashMap map = new HashMap();
        map.put("roleid",roleid);
        map.put("menuids",mid);
        int i = roleMapper.insertMiddle(map);
        return i;
    }

    @Override
    public int insertSelective(Role record) {
        return 0;
    }

    @Override
    public Role selectByPrimaryKey(Integer roleid) {
        //1.查询出角色信息
        Role role = roleMapper.selectByPrimaryKey(roleid);
        //2.查询角色所对应的菜单信息
        List<Integer> list = menuMapper.findmenuids(roleid);
        role.setMenuids(list);

        return role;
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Role record) {
        return 0;
    }
}
